<?php
namespace App\Common\Lib\Com;

use App\Common as com;
use App\Common\Lib\Com\ChannelBlockingQueue as thisChannelBlockingQueue;

class ChannelBlockingQueue extends com\Lib\LibBase {


	/**
	 * @var \Swoole\Coroutine\Channel
	 */
	private \Swoole\Coroutine\Channel $channel;

	/**
	 * @param int $maxQueueLength 队列最大长度
	 */
	public function __construct( private int $maxQueueLength ) {
		//
		$this->channel = new \Swoole\Coroutine\Channel( $maxQueueLength );
		//
		parent::__construct();
	}

	public function getMaxQueueLength(  ):int {
		return $this->maxQueueLength;
	}

	/**
	 * 进入队列
	 * @param float $timeoutS 超时时间，秒【支持浮点型，如 1.5 表示 1s+500ms】。[-1][默认]不限制
	 * @return bool
	 */
	public function join( float $timeoutS = -1 ):bool {
		return $this->channel->push( true ) === true;
	}

	/**
	 * 离开队列
	 * @return mixed
	 */
	public function leave():bool{
		return $this->channel->pop( 0 ) === true;
	}

	// ===============================
	//
	//
	// 下面为静态方法
	//
	//
	// ===============================

	/**
	 * @param int $maxQueueLength 队列最大长度
	 * @return $this
	 */
	public static function makeOfNew( int $maxQueueLength ):self {
	    return self::make( ...func_get_args() );
	}

}
